package com.ogawa.a7517.fragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ogawa.a7517.Constant;
import com.ogawa.a7517.R;
import com.ogawa.a7517.adapter.AutoAdapter;
import com.ogawa.a7517.bean.DeviceStateBean;
import com.ogawa.a7517.bean.MassageArmchair;
import com.ogawa.a7517.bean.ProgramBean;
import com.ogawa.a7517.utils.ViewUtil;
import com.ogawa.a7517.widget.SpaceItemDecoration;
import com.ogawa.base.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class AutoPageFragment extends BaseFragment {
    private List<ProgramBean> programBeans;
    private AutoAdapter adapter;

    public AutoPageFragment(List<ProgramBean> programBeans) {
        this.programBeans = programBeans;
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_recyclerview;
    }

    @Override
    public void initView(View view) {
        RecyclerView rlvProgram = view.findViewById(R.id.rlv_common);
        GridLayoutManager layoutManager = new GridLayoutManager(activity7517, 2);
        rlvProgram.setLayoutManager(layoutManager);
        FrameLayout.MarginLayoutParams lp = (FrameLayout.MarginLayoutParams) rlvProgram.getLayoutParams();
        lp.topMargin = ViewUtil.dip2px(10, activity7517);
        rlvProgram.setLayoutParams(lp);
        int topSpace = ViewUtil.dip2px(8, activity7517);
        int leftSpace = ViewUtil.dip2px(40, activity7517);
        SpaceItemDecoration itemDecoration = new SpaceItemDecoration(topSpace, leftSpace, 2);
        rlvProgram.addItemDecoration(itemDecoration);
        adapter = new AutoAdapter(programBeans, true);
        rlvProgram.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter1, view1, position) -> {
            if (view1.isSelected()) {
                judgeToWhere(false);
            } else {
                activity7517.publish(Constant.AUTO_PROGRAM, String.valueOf(programBeans.get(position).getModel()));
            }
        });
        deviceStateChange();
    }

    @Override
    public void deviceStateChange() {
        super.deviceStateChange();
        DeviceStateBean stateBean = MassageArmchair.getInstance().getDeviceStatusBean();
        if (stateBean != null) {
            if (stateBean.getFunctions().getAutoProgram() != null) {
                int auto = StringUtils.getValue(stateBean.getFunctions().getAutoProgram().getModelValue());
                adapter.setRunning(auto);
            }
        } else {
            if (adapter != null) {
                adapter.setRunning(0);
            }
        }
    }
}
